
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 * @author Team Name: ByteCode
 * Team members: Kevin Conner, Vinesh Thakur, and James Yu
 * MancalaPitComponent class holds all the data for the mancala game
 */
public class MancalaPitComponent extends JComponent{
   int radius = 50;
   int x = 50;
   int y = 50;
   int hash = 0;
   MancalaDataModel m = null;
   //default will be normal strategy.
   MancalaStrategy s = new MancalaStrategyNormal();
   public MancalaPitComponent( final MancalaDataModel mdm){
      super();
      m=mdm;
      this.addMouseListener(new
            MouseAdapter()
      {
         public void mousePressed(MouseEvent event)
         {
            mdm.rules(hash);
            repaint();
         }});
      mdm.attach(new
            ChangeListener()
      {@Override
         public void stateChanged(ChangeEvent e) {
         repaint();

      }});
   }

   /**
    * MancalaPitComponent initializes pit 
    * @param x int pos
    * @param y int pos
    * @param hash int pit value
    * @param mdm MancalDataModel of data Model of the game
    */
   public MancalaPitComponent(int x, int y, int hash,MancalaDataModel mdm){
      this(mdm);
      this.x = x;
      this.y = y;
      this.hash = hash;
   }
   
   /**
    * setStrategy sets strategy of GUI
    * @param strategy MancalaStrategy 
    */
   public void setStrategy(MancalaStrategy strategy){
      s = strategy;
   }

   /**
    * paintComponent draws pits and stones
    */
   public void paintComponent(Graphics g){
      Dimension preferredSize = new Dimension(x+1,y+1);
      super.paintComponent(g);
      Graphics2D g2 = (Graphics2D)g;
      this.setSize(x+1, y+1);
      this.setBackground(s.getBackGroundColor());
      this.setPreferredSize(preferredSize);
      this.setMaximumSize(preferredSize);
      this.setMinimumSize(preferredSize);
      Rectangle2D rectangle = new Rectangle2D.Double(0,0,x,y);
      g2.draw(rectangle);
      g2.setColor(s.getInsidePitColor());
      g2.fill(rectangle);
      Ellipse2D ellipse = new Ellipse2D.Double(0,0,x,y);
      g2.setColor(s.getOutsidePitColor());
      g2.fill(ellipse);
      g2.setColor(s.getFontColor());
      g2.drawString(((Integer)m.getPitCount(hash)).toString(), x/2, y-5);
      
      for (int z = 0; z < m.getPitCount(hash); z++ ){
    	  int collumn = z/5;
    	  int row = z%5;
    	  g2.setColor(s.getBeadFill());
    	  g2.fill(new Ellipse2D.Double(y/5+(row*6),10+(collumn*6),5,4));
      }

   }
   
   /**
    * getPitHash method returns position of pit
    * @return int value of pit
    */
   public int getPitHash(){
      return hash;
   }
}
